import com.alibaba.fastjson.JSON
import com.o2o.utils.Iargs
import org.apache.spark.sql.SparkSession

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/6/5 16:23
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object InsertESPData {
  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName(s"${this.getClass.getSimpleName}")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("cluster.name","O2OElastic")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", Iargs.OBSACCESS)
    sc.hadoopConfiguration.set("fs.s3a.secret.key", Iargs.OBSSECRET)
    sc.hadoopConfiguration.set("fs.s3a.endpoint", Iargs.OBSENDPOINT)
    sc.setLogLevel("WARN")

    import org.elasticsearch.spark._
    val yearArr = Array("2020")
    val monthArr = Array("8")
    val platArr = Array("jd","tmall","taobao","suning")
    for (year <- yearArr){
      for(month <- monthArr){
        for(plat <- platArr) {
          println(year + "---" + month + "---"+plat+"----start!!!")
          var index = s"${year}_${plat}_jd_xianyou/${plat}_jd_xianyou_${year}_${month}"

          val data1 = sc.esJsonRDD(index).values

          data1.map(line => {
            val nObject = JSON.parseObject(line)
            nObject.remove("priceText_bak")
            nObject
          }).saveToEs(index,
            Map("es.mapping.id" -> "good_id",
              "es.nodes" -> "172.23.0.32",
              "es.nodes.wan.only" -> "true",
              "es.port" -> "9220",
              "cluster.name" -> "jd_elastic"))
        }
      }
    }




  sc.stop()
}
}
